<?php

namespace app\admin\controller\shop\delivery;

use app\admin\controller\shop\Base;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Throwable;

/**
 * 物流公司管理
 */
class Company extends Base
{
    /**
     * Company模型对象
     * @var object
     * @phpstan-var \app\admin\model\shop\delivery\Company
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['name'];

    protected array $noNeedPermission = ['company'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\shop\delivery\Company;
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */

    /**
     * 远程搜索
     * @return void
     * @throws Throwable
     * @throws DbException
     */
    public function select(): void
    {
        list($where, $alias, $limit, $order) = $this->queryBuilder();
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->where('status', 1)
            ->order($order)
            ->paginate($limit);

        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * 获取物流公司列表
     * @return void
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function company(): void
    {
        $list = $this->model->where('status', 1)->withoutField(['create_time', 'update_time'])->select();

        $this->success('', [
            'list' => $list
        ]);
    }
}